import { Injectable } from '@angular/core';
import { HttpClient } from '../../../providers/httpClient';
import { Config } from '../../../providers/config';
import _ from 'lodash';
@Injectable()
export class BcwRuleService {
	constructor(
		public httpClient: HttpClient,
		public config: Config
	) { }

	//获取用户能订阅的规则
  getRuleOfUserAll(loginID) {
    let postBody = {
      "module": "KSB",
      "method": "BCW_A02_0200",
      "format": "json",
      "params": {
        "vc_oa_username":loginID
      }
    };
    return this.httpClient.post(postBody, {
      isShowLoading: true
    })
  }
  //获取用户不需要推送的规则列表
  getSubScribeRule(vc_usrnm) {
    let postBody = {
      "module": "KSB",
      "method": "BCW_A02_0280",
      "format": "json",
      "params": {
        vc_usrnm:vc_usrnm
      }
    };
    return this.httpClient.post(postBody, {
      isShowLoading: false
    })
  }
  //发送邮件更改是否订阅
  editEmail(data, type) {
    if (!data.mailbosid) {
      delete data.mailbosid
    }
    let postBody = {
      "module": "Email",
      "method": type + "Email",
      "params": {
        "mailInfoDto": data
      }
    };
    if (type === "draft") {
      postBody.method = "draftEmailNoFile";
    }
    console.log("editEmail.postBody", postBody);
    return this.httpClient.post(postBody);
  }
  // updateRuleFlag(param){
  //   let postBody = {
  //     "module": "KSB",
  //     "method": "BCW_A02_0290",
  //     "format": "json",
  //     "params": param
  //   };
  //   return this.httpClient.post(postBody, {
  //     isShowLoading: true
  //   })
  // }


  /**
   * [formatContactData 把CN=A938493525/TI=ybyg/OU=xtyfz 和  系统研发组/一般员工/冯镜蒯 对应上]
   * @param {[type]} code [CN=A938493525/TI=ybyg/OU=xtyfz]
   * @param {[type]} name [系统研发组/一般员工/冯镜蒯]
   */
  formatContactData(code, name) {
    let result = [];
    if (code) {
      code = code.split(',');
      name = name.split(',');
      _.each(code, function(item, index) {
        let contact: any = {};

        _.each(item.split('/'), function(t, i) {
          let temp = t.split('=');
          if (temp[0] == 'CN') {
            contact.empid = temp[1];
          } else if (temp[0] == 'TI') {
            contact.titleid = temp[1];
          } else if (temp[0] == 'OU') {
            contact.orgid = temp[1];
          }
        });

        contact.name = name[index];
        contact.full_name = name[index];
        if (contact.empid) {
          contact.type = 'user';
        } else if (contact.titleid) {
          contact.type = 'title';
        } else {
          contact.type = 'dept';
        }
        result.push(contact);
      });
      return result;
    } else {
      return result;
    }
  }

  getRuleOfUser() {
	  let postBody = {
      "module": "OAServer",
      "method": "getRuleOfUser",
      "params": {

      }
    };

	  return this.httpClient.post(postBody, {
	    isShowLoading: true
    })
  }

  saveBcwRuleNotify(data, flag) {
	  let postBody = {
      "module": "OAServer",
      "method": "saveBcwRuleNotify",
      "params": {
        "data": data,
        "flag": flag
      }
    };

	  return this.httpClient.post(postBody, {
	    isShowLoading: false
    })
  }


}
